//
//  LGECGDetailViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/12.
//  心电图1

#import "LGECGDetailViewController.h"
#import "LGGetWebViewController.h"
#import "LGECGDetailResultCell.h"
#import "LGECGDetailDescCell.h"
#import "LGECGDetailChartView.h"

#define  itemW  ((Screen_W - viewPix(32))/75.0)

@interface LGECGDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**   */
@property (nonatomic , strong)UILabel *timeLabel;
/**   */
@property (nonatomic , strong)UIButton *checkBtn;
/**   */
@property (nonatomic , strong)UILabel *valueLabel;
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGECGDetailChartView *chartView;
/**   */
@property (nonatomic , strong)NSArray *resultArray;
/**   */
@property (nonatomic , copy)NSString *descContent;
/**   */
@property (nonatomic , copy)NSString *reportUrl;

@end

@implementation LGECGDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"心电分析";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
    self.emptyView.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.Physicalsign/ecgDetail" parameters:@{@"patient_id":self.patientid,@"ecg_id":self.ecgId} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.reportUrl = LGNSString(returnData[@"airesult"][@"report_url"]);
            self.dateLabel.text = LGNSString(returnData[@"airesult"][@"record_date"]);
            self.valueLabel.text = [NSString stringWithFormat:@"%@/min",returnData[@"airesult"][@"ecg_value"]];
            self.resultArray = returnData[@"airesult"][@"diasList"];
            self.descContent = LGNSString(returnData[@"airesult"][@"desc"]);
            [self.tableView reloadData];
            NSArray *pointArray = returnData[@"airesult"][@"ecg_datas"];
            CGFloat width = pointArray.count*(itemW/5.0)>Screen_W ? pointArray.count*(itemW/5.0) : Screen_W;
            self.scrollView.contentSize = CGSizeMake(width, itemW*5*6);
            self.chartView.frame = CGRectMake(0, 0, width, itemW*5*6);
            self.chartView.dataArray = pointArray;
            [self.chartView setNeedsDisplay];
        }else{
            [self.emptyView showViewWithImage:@"emptyView2" content:message offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
    }];
}

//查看分析报告
-(void)checkReportAction{
    LGGetWebViewController *controller = [[LGGetWebViewController alloc]init];
    controller.title = @"分析报告";
    controller.urlStr = self.reportUrl;
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section == 0){
        return self.resultArray.count;
    }else if(section == 2){
        return 1;
    }
    return 0;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        LGECGDetailResultCell *resultCell = [tableView dequeueReusableCellWithIdentifier:@"resultCell" forIndexPath:indexPath];
        resultCell.selectionStyle = UITableViewCellSelectionStyleNone;
        resultCell.dataDic = self.resultArray[indexPath.row];
        return resultCell;
    }else if (indexPath.section == 2){
        LGECGDetailDescCell *descCell = [tableView dequeueReusableCellWithIdentifier:@"descCell" forIndexPath:indexPath];
        descCell.selectionStyle = UITableViewCellSelectionStyleNone;
        descCell.content = self.descContent;
        return descCell;
    }
    return nil;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CGFloat height = (section == 1)?(viewPix(66)+itemW*5*6):viewPix(56);
    LGCornerHeaderView *headView = [[LGCornerHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, height)];
    headView.topMargin = viewPix(8);
    NSArray *titleArray = @[@"分析结果",@"平均心率",@"分析说明"];
    UIButton *titleBtn = [UIButton buttonWithTitle:titleArray[section] titleFont:LGFontWeight(15, UIFontWeightMedium) textColor:@"#333333" imageName:[NSString stringWithFormat:@"心电分析-%@",titleArray[section]]];
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    titleBtn.frame = CGRectMake(viewPix(28) , viewPix(8) , viewPix(90), viewPix(48));
    titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    titleBtn.userInteractionEnabled = NO;
    [headView addSubview:titleBtn];
    if(section == 1){
        [headView addSubview:self.valueLabel];
        [headView addSubview:self.scrollView];
        [self.scrollView addSubview:self.chartView];
    }
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 1){
        return viewPix(66)+itemW*5*6;
    }
    return viewPix(56);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footerView.backgroundColor = [UIColor clearColor];
    footerView.bottomMargin = viewPix(8);
    return footerView;
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGECGDetailResultCell class] forCellReuseIdentifier:@"resultCell"];
        [_tableView registerClass:[LGECGDetailDescCell class] forCellReuseIdentifier:@"descCell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(viewPix(28), viewPix(56), Screen_W-viewPix(56), itemW*5*6)];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
    }
    return _scrollView;
}

-(LGECGDetailChartView *)chartView{
    if(!_chartView){
        _chartView = [[LGECGDetailChartView alloc] initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), itemW*5*6)];
    }
    return _chartView;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _dateLabel.frame = CGRectMake(viewPix(19), viewPix(16), viewPix(170), viewPix(22));
    }
    return _dateLabel;
}

-(UILabel *)timeLabel{
    if(!_timeLabel){
        _timeLabel = [UILabel lableWithText:@"时长 00:00:30" colorString:@"#333333" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _timeLabel.frame = CGRectMake(viewPix(19), viewPix(40), viewPix(120), viewPix(23));
    }
    return _timeLabel;
}

-(UIButton *)checkBtn{
    if(!_checkBtn){
        _checkBtn = [UIButton buttonWithTitle:@"查看智能分析报告" titleFont:LGFontWeight(14, UIFontWeightMedium) normalColor:@"#FFFFFF" selectColor:@"#FFFFFF"];
        _checkBtn.frame = CGRectMake(Screen_W-viewPix(160), viewPix(24), viewPix(144), viewPix(32));
        _checkBtn.backgroundColor = [UIColor colorWithString:@"#3291FF"];
        _checkBtn.cornerRidus = viewPix(16);
        [_checkBtn addTarget:self action:@selector(checkReportAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _checkBtn;
}

-(UILabel *)valueLabel{
    if(!_valueLabel){
        _valueLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _valueLabel.frame = CGRectMake(viewPix(128), viewPix(8), viewPix(150), viewPix(48));
    }
    return _valueLabel;
}

-(UIView *)tableHeaderView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(75))];
    headView.backgroundColor = [UIColor clearColor];
    [headView addSubview:self.dateLabel];
    [headView addSubview:self.timeLabel];
    [headView addSubview:self.checkBtn];
    return headView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
